<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP6_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: 牧羊人 <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\adminmini\controller;


use app\adminmini\service\DoorlistService;
use app\common\library\Men;
use think\facade\Db;
use think\facade\Log;
use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;

/**
 * 门禁列管理-控制器
 * @author 牧羊人
 * @since: 2022/01/23
 * Class Doorlist
 * @package app\admincustomer\controller
 */
class Door extends Backend
{
    /**
     * 初始化方法
     * @author 牧羊人
     * @since: 2022/01/23
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->service = new DoorlistService();
    }

    public function getCradlcassList()
    {
        $where['store_id'] = $this->userId();
        $where['pid'] = 0;
        $plist = Db::name('customer_door_list')->where('mark', 1)->where($where)->select();
        $data = array();
        if (count($plist) > 0) {
            foreach ($plist as $i => $v) {
                $data[] = array(
                    'id' => $v['id'],
                    'name' => $v['name']
                );
            }
        }
        return message("操作成功", true, $data);
    }

    public function open()
    {
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return message("记录ID不能为空", false);
        }
        $info = Db::name('customer_door_list')->where('id', $ids)->find();

        $appkey = Db::name('customer_set_cabinet')->where('store_id', $info['store_id'])->find();
        $door_list_data = $appkey;
        $door_list_data['equipment_id'] = $info['door_id'];
        $res = $this->newopen($info['door_id'], 0);
        if ($res['code'] == '200') {
            // 记录日子
            $data = array(
                'add_time' => time(),
                'end_time' => time() + $appkey['admin_time'] * 60,
                'door_id' => $info['door_id'],
            );
            Db::name('coutomer_door_list_data')->insert($data);
        }
        return $res;


    }
    /**
     * 开门
     * @param $door_list
     * @param $admin_id
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public static function newopen($deviceCode, $type)
    {
        header("Content-type: text/html; charset=utf-8");
        $encryptionStr = strtoupper(md5('deviceCode=' . $deviceCode));
        $apiurl = "https://qsg.wuminginfo.com/api/device/generateTask?encryptionStr=" . $encryptionStr . "&deviceCode=" . $deviceCode . "&type=" . $type;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        $data = curl_exec($ch);
        return json_decode($data, true);
    }

    public function openend()
    {
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return message("记录ID不能为空", false);
        }
        $info = Db::name('customer_door_list')->where('id', $ids)->find();

        $appkey = Db::name('customer_set_cabinet')->where('store_id', $info['store_id'])->find();
        $door_list_data = $appkey;
        $door_list_data['equipment_id'] = $info['door_id'];
        return $this->newopen($info['door_id'], 4);
    }

}